//
//  DesignItemPickerViewController.m
//  Surveyer
//
//  Created by Qikun Guo on 13-8-8.
//  Copyright (c) 2013年 Tim. All rights reserved.
//

#import "DesignItemPickerViewController.h"
#import "TKLabelTextFieldCell.h"
#import "TKLabelFieldCell.h"
#import "PickerView.h"
#import "TypeRedirector.h"

@interface DesignItemPickerViewController ()

@end

@implementation DesignItemPickerViewController
@synthesize currentItem, pickerView;
- (id)initWithStyle:(UITableViewStyle)style
{
    if (self = [super initWithStyle:style])
    {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = NSLocalizedString(@"Question", nil);
    
    once = YES;
    flag = NO;
    if (currentItem.type)
        [pickerView selectRow:[TypeRedirector indexOfType:currentItem] inComponent:0 animated:YES];
    keyValueDict = [TypeRedirector dictionaryOfConfiguration:currentItem];
    allKeys = [[keyValueDict allKeys] sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:nil ascending:YES]]];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[SurveyerStore defaultStore] saveChanges];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

#pragma mark - Table view

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            //typePicker
            return 2;
            break;
            
        case 1:
            return keyValueDict.count;
            break;
            
        default:
            return 2;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return NSLocalizedString(@"Basic", @"section name");
            break;
            
        case 1:
            return NSLocalizedString(@"Survey", @"2nd section name");
            break;
            
        default:
            return nil;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                {
                    TKLabelTextFieldCell *cell = [[TKLabelTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TText"];
                    cell.delegate = self.currentItem;
                    cell.label.text = NSLocalizedString(@"Title", nil);
                    cell.key = @"title";
                    return cell;
                    break;
                }
                default:
                {
                    if (!flag)
                    {
                        //normal situation
                        TKLabelFieldCell *cell = [[TKLabelFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TLabel"];
                        cell.label.text = NSLocalizedString(@"Type", nil);
                        cell.field.text = currentItem.type;
                        cell.imageView.Image = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@", @"thumbnail_", currentItem.type]];
                        return cell;
                    }
                    else
                    {
                        //expand view
                        TKLabelFieldCell *cell = [[TKLabelFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TLabel_"];
                        cell.label.text = NSLocalizedString(@"Type", nil);
                        [cell.contentView addSubview:pickerView];
                        if (once)
                        {
                            pickerView.frame = CGRectOffset(pickerView.frame, 0, 35);
                            once=NO;
                        }
                        return cell;
                    }
                    break;
                }
            }
            break;
        }
        case 1:
        {
            //survey specific area
            TKLabelTextFieldCell *cell = [[TKLabelTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TText"];
            cell.delegate = self.currentItem;
            cell.label.text = [allKeys objectAtIndex:indexPath.row];
            cell.key = [keyValueDict valueForKey:cell.label.text];
            return cell;
            return nil;
            break;
        }
        default:
        {
            if (indexPath.row==0)
            {
                UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                cell.textLabel.text = NSLocalizedString(@"Preview", nil);
                cell.textLabel.textColor = [UIColor colorWithRed:16./255 green:95./255 blue:254./255 alpha:1];
                return cell;
            }
            else
            {
                UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                cell.textLabel.text = NSLocalizedString(@"Delete Item", @"button in bottom");
                cell.textLabel.textColor = [UIColor redColor];
                return cell;
                break;
            }
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 1)
    {
        flag = !flag;
        [self.tableView beginUpdates];
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
         [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];
    }
    else if (indexPath.section == 2)
    {
        if (indexPath.row==0)
        {
            [[SurveyerStore defaultStore] saveChanges];
            [self.navigationController pushViewController:[TypeRedirector viewControllerOfItem:currentItem] animated:YES];
        }
        else
        {
            [currentItem.owner removeItemsObject:currentItem];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 1 && flag)
        return 55 + pickerView.bounds.size.height;
    return 55;
}

#pragma mark - Picker view
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [TypeRedirector name].count;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //with image named "thumbnailBinary", etc.
    PickerView *pv = [[PickerView alloc] initWithTitle:[[TypeRedirector name] objectAtIndex:row] image:[UIImage imageNamed:[NSString stringWithFormat:@"%@%@", @"thumbnail_", [[TypeRedirector name] objectAtIndex:row]]]];
    return pv;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    currentItem.type = [[TypeRedirector name] objectAtIndex:row];
    keyValueDict = [TypeRedirector dictionaryOfConfiguration:currentItem];
    allKeys = [[keyValueDict allKeys] sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:nil ascending:YES]]];
}

#pragma mark - @selectors
- (void)clearValue:(NSInteger)min
{
    if (min<=4)
    {
        currentItem.value4 = nil;
        if (min<=3)
        {
            currentItem.value3 = nil;
            if (min<=2)
            {
                currentItem.value2 = nil;
                if (min<=1)
                {
                    currentItem.value1 = nil;
                }
            }
        }
    }
}
@end